home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / dirscan.com / DIRSCAN.C next >
Encoding:
C/C++ Source or Header  |  1989-04-01  |  2.5 KB  |  113 lines

  1. /******
  2.  
  3.     Name:       dirscan
  4.     Author:     Jim Garrison (76247,1747)
  5.     Function:   Recursively scans a DOS file directory tree structure.
  6.  
  7. ******/
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <dos.h>
  12.  
  13. #define FNULL ((void (*)()) NULL)
  14. #define DNULL ((void *) NULL)
  15.  
  16. long dirscan
  17.   (
  18.     char directory[],                         /*  [d:][directory-path]    */
  19.     char file_mask[],                         /*  File Name Mask         */
  20.     char attrib,                             /*  File Attributes Mask    */
  21.     void (* dir_exit) (char *),              /*  Directory Exit Pointer */
  22.     void (* file_exit)(char *,struct find_t) /*  File Exit Point        */
  23.   )
  24.  
  25. {
  26.  
  27.     unsigned       rc;
  28.     long           nfiles = 0;
  29.     struct find_t  dos_find_buffer;
  30.     char           path_work[_MAX_PATH];
  31.     char           search_att;
  32.  
  33.     /*-- Mask off unwanted bits in attrib flags --*/
  34.  
  35.     search_att = attrib & (_A_HIDDEN | _A_SYSTEM);
  36.  
  37.     /*-- Call Directory-handling exit if specified --*/
  38.  
  39.     if (dir_exit != FNULL) dir_exit(directory);
  40.  
  41.     /*-- Build complete path to find files in current directory --*/
  42.  
  43.     strcpy(path_work,directory);
  44.     strcat(path_work,file_mask);
  45.  
  46.     /*-- Examine "normal" files matching file_mask in cur dir --*/
  47.  
  48.     rc = _dos_findfirst(path_work,           /* Complete path name */
  49.                         search_att,        /* Normal Files only  */
  50.                         &dos_find_buffer); /* Work area pointer  */
  51.  
  52.     while(rc==0)
  53.     {
  54.  
  55.         /*-- Call File-handling exit if specified --*/
  56.  
  57.         if (file_exit != FNULL) file_exit(directory,dos_find_buffer);
  58.  
  59.         /*-- Get next matching file --*/
  60.  
  61.         rc = _dos_findnext(&dos_find_buffer);
  62.  
  63.     }
  64.  
  65.     /*-- Now examine all sub-directories in current dir --*/
  66.  
  67.     strcpy(path_work,directory);
  68.     strcat(path_work,"*.*");
  69.  
  70.     rc = _dos_findfirst(path_work,_A_SUBDIR,&dos_find_buffer);
  71.  
  72.     while(rc==0)
  73.     {
  74.  
  75.         /*-- scan only subdirectory entries this time --*/
  76.  
  77.         if (dos_find_buffer.attrib & _A_SUBDIR)
  78.         {
  79.  
  80.             /*-- skip 'parent' and 'this dir' entries --*/
  81.  
  82.             if (dos_find_buffer.name[0] != '.')
  83.             {
  84.  
  85.                 /*-- Build sub-directory path name --*/
  86.  
  87.                 strcpy(path_work,directory);
  88.                 strcat(path_work,dos_find_buffer.name);
  89.                 strcat(path_work,"\\");
  90.  
  91.                 /*-- Go search sub-directory recursively, --*/
  92.                 /*-- accumulating number of files examined --*/
  93.  
  94.                 nfiles += dirscan(path_work,   /* Subdir Path Name      */
  95.                                   file_mask,   /* File Name Mask      */
  96.                                   attrib,       /* File Attribute Mask */
  97.                                   dir_exit,    /* Dir Exit Routine      */
  98.                                   file_exit);  /* File Exit Routine   */
  99.  
  100.             }
  101.  
  102.         }
  103.         else
  104.             nfiles++;
  105.  
  106.         rc = _dos_findnext(&dos_find_buffer);
  107.  
  108.     }
  109.  
  110.     return(nfiles);
  111.  
  112. }
  113.